﻿<cfsilent>


	<cfscript>
		
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");

		response = structNew();

		taskId = event.getArg("TSK");
		taskType = event.getArg("TT");

		weekDay = event.getArg("wkday");
		lectureId = event.getArg("lec");
		cells = event.getArg("n");
		
		entity = "";
		
		transaction {
			
			/* 载入教学任务 */
			switch(taskType){
				case 'P':
					sql = "SELECT  a.task_time,
								   a.task_place,
								   a.task_week
							FROM t_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'C':
					sql = "SELECT  a.task_time,
								   a.task_place,
								   a.task_week
							FROM t_common_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'R':
					sql = "SELECT  a.task_time,
								   a.task_place,
								   a.task_week
							FROM t_reopen_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'T':
					sql = "SELECT  a.task_time,
								   a.task_place,
								   a.task_week
							FROM t_sport_task a 
							WHERE a.tsk_id = :taskId ";
					break;
			}
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			rs_task = queryObj.execute(sql = sql).getResult();
			/* 载入排课任务 */
			sql = "SELECT  a.task_id,a.period_done,
						   a.period_need 
					FROM t_task_arrange a 
					WHERE a.task_id = :taskId ";
					
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			rs_taskArrange = queryObj.execute(sql = sql).getResult();
			
			targetTime = weekDay & "@" & lectureId & "-" & cells;
			
			pos = listFind(rs_task.task_time, targetTime, ",");
			
			taskTime = listDeleteAt(rs_task.task_time, pos, ",");
			
			if (listLen(rs_task.task_place)) {
				taskPlace = listDeleteAt(rs_task.task_place, pos, ",");
			} else {
				taskPlace= "";	
			}
			
			
			/* 删除教学班时间占用情况表 */
			
			sql = "	DELETE FROM
					t_task_timetable ttt
				WHERE
					ttt.task_id = :taskId 
					AND
					ttt.tasktime = :taskTime ";
		
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="taskTime", value=targetTime, cfsqltype="cf_sql_varchar");
			queryObj.execute(sql = sql);
				
			
			
			/* 删除地点时间占用表 */
			
			sql = "	DELETE FROM
					t_clsroom_timetable ttt
					WHERE ttt.crtime_id IN (
						  SELECT cf.crtime_id
						  FROM t_clsroom_forcourse cf
						  WHERE cf.task_id = :taskId 
						  		AND 
								cf.tasktime = :taskTime )";

			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="taskTime", value=targetTime, cfsqltype="cf_sql_varchar");
			queryObj.execute(sql = sql);
			

			/* 删除教师时间占用表 */
			
			sql = "	DELETE FROM
					t_teacher_timetable ttt
					WHERE
						ttt.task_id = :taskId 
						AND
						ttt.tasktime = :taskTime ";
		
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="taskTime", value=targetTime, cfsqltype="cf_sql_varchar");
			queryObj.execute(sql = sql);
			
			
			/* 保存排课时间 */
			switch(taskType){
					case 'C':
						sql = "	UPDATE
									t_common_task t
								SET
									t.task_time = :TaskTime ,
									t.task_place = :TaskPlace 
								WHERE
									t.tsk_id = :taskId ";
						break;
					case 'P':
						sql = "	UPDATE
									t_task t
								SET
									t.task_time = :TaskTime ,
									t.task_place = :TaskPlace 
								WHERE
									t.tsk_id = :taskId ";
						break;
					case 'R':
						sql = "	UPDATE
									t_reopen_task t
								SET
									t.task_time = :TaskTime ,
									t.task_place = :TaskPlace 
								WHERE
									t.tsk_id = :taskId ";
						break;
					case 'T':
						sql = "	UPDATE
									t_sport_task t
								SET
									t.task_time = :TaskTime ,
									t.task_place = :TaskPlace 
								WHERE
									t.tsk_id = :taskId ";
						break;
				}
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				queryObj.addParam(name ="TaskTime", value=taskTime, cfsqltype="cf_sql_varchar");
				queryObj.addParam(name ="TaskPlace", value=taskPlace, cfsqltype="cf_sql_varchar");
				queryObj.execute(sql = sql);
			
			/* 重新计算已排周学时 */
			taskWeek = listToArray(rs_task.task_week, "-");
			periodWeeks = int(taskWeek[2]) - int(taskWeek[1]) + 1;
			
			periodDone = rs_taskArrange.period_done - periodWeeks * cells;
			/* 修复排课学时为负值 */
			if ( periodDone lt 0 ) { periodDone = 0; }

			sql = "	UPDATE
							t_task_arrange t
						SET
							t.period_done = :PeriodDone 
						WHERE
							t.task_id = :taskId ";
				
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "taskId", value = taskId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name = "PeriodDone", value = periodDone, cfsqltype="cf_sql_interger");
			queryObj.execute(sql = sql);
			
			structInsert(response, "TaskID", taskId);
			structInsert(response, "WeekDay", weekDay);
			structInsert(response, "CellStart", lectureId);
			structInsert(response, "CellLength", cells);
		}
		
		transaction {
			
			/* 载入教学任务 */
			switch(taskType){
				case 'P':
					sql = "SELECT  a.task_capacity
							FROM t_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'C':
					sql = "SELECT  a.task_capacity
							FROM t_common_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'R':
					sql = "SELECT  a.task_capacity
							FROM t_reopen_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'T':
					sql = "SELECT  a.task_capacity
							FROM t_sport_task a 
							WHERE a.tsk_id = :taskId ";
					break;
			}
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			rs_taskNow = queryObj.execute(sql = sql).getResult();
			
			TaskCapacity = rs_taskNow.task_capacity;
			
			/* 刷新教学班最大容量 */
			if ( taskPlace eq "" ) {
				/* 如果没有排地点 则恢复容量为默认值*/ 
			TaskCapacity = 0;
			}
			else {
				/* 从已拍地点中读取最小值 */
				sql = "SELECT 
						min(c.sits_course) sits_course
						FROM t_clsroom_timetable a 
							INNER JOIN t_classroom c ON c.cr_id = a.cr_id 
							INNER JOIN t_clsroom_forcourse cf ON a.crtime_id = cf.crtime_id 
						WHERE cf.task_id  = :taskId ";
									
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				rs_leftClassroom = queryObj.execute(sql = sql).getResult();
				if ( rs_leftClassroom.recordCount) {
					TaskCapacity = rs_leftClassroom["sits_course"][1];
				}
			}
			switch(taskType){
				case 'P':
					sql = "UPDATE t_task a 
							SET a.task_capacity = :TaskCapacity 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'C':
					sql = "UPDATE t_common_task a 
							SET a.task_capacity = :TaskCapacity 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'R':
					sql = "UPDATE t_reopen_task a 
							SET a.task_capacity = :TaskCapacity 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'T':
					sql = "UPDATE t_sport_task a 
							SET a.task_capacity = :TaskCapacity 
							WHERE a.tsk_id = :taskId ";
					break;
			}
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name ="TaskCapacity", value=TaskCapacity, cfsqltype="cf_sql_interger");
			queryObj.execute(sql = sql);
		}
				
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>